package com.baibei.pay.recharge;

import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.model.RechargeAmountInfo;
import com.baibei.module.OnItemSelectListener;
import com.baibei.pay.R;
import com.baibei.pay.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by shenminjie on 2017/10/30.
 * mail:shenminjie@100bei.com
 */

class AmountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<RechargeAmountInfo> mInfos;
    private OnItemSelectListener<RechargeAmountInfo> mOnItemSelectListener;

    void setInfos(List<RechargeAmountInfo> infos) {
        this.mInfos = infos;
    }

    List<RechargeAmountInfo> getInfos() {
        return mInfos;
    }

    void setOnItemSelectListener(OnItemSelectListener<RechargeAmountInfo> mOnItemSelectListener) {
        this.mOnItemSelectListener = mOnItemSelectListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_amount, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            final RechargeAmountInfo info = mInfos.get(position);
            int bgResource = R.drawable.bg_amount_select;
            int textColor = android.R.color.white;
            if (!info.isSelect()) {
                bgResource = R.drawable.bg_amount_unselect;
                textColor = R.color.textPrimary;
            }
            viewHolder.tvAmount.setBackgroundResource(bgResource);
            viewHolder.tvAmount.setText(String.valueOf(info.getAmount()));
            viewHolder.tvAmount.setTextColor(ResourcesCompat.getColor(viewHolder.itemView.getResources(), textColor, null));
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemSelectListener != null) {
                        mOnItemSelectListener.onItemSelectListener(v, info, position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mInfos == null ? 0 : mInfos.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_amount)
        TextView tvAmount;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
